import re

def domain_re():
    # 使用with语句打开文件，它会自动关闭文件
    with open('fenzhan.txt', 'r', encoding='utf-8') as file:
        fzlines = file.readlines()  # 读取所有行

    # 使用with语句打开文件，它会自动关闭文件
    with open('cms-links.txt', 'r', encoding='utf-8') as file:
        lines = file.readlines()  # 读取所有行

    new_lines=[]

    for item in fzlines:
        new_item = item.replace('\n', '')
        # 替换域名
        new_lines = [line.replace(r'https://www.jiangshuntech.com', new_item) for line in lines]

        # 将修改后的内容写回文件或打印
        pattern = r'https://([^\.]+)\.'
        match = re.search(pattern, new_item)
        if match:
            print(match.group(1))  # 输出 'anhui'
            with open('cms-links.txt', 'a', encoding='utf-8') as file:
                file.writelines(new_lines)
    print('Success!')

def del_chongfu():
    # 读取文件并去除重复的行
    with open('cms-links.txt', 'r', encoding='utf-8') as file:
        lines = file.readlines()

    # 使用集合去除重复的行，保持原始顺序
    unique_lines = list(dict.fromkeys(lines))

    # 将去除重复后的行写回文件
    with open('cms-links.txt', 'w', encoding='utf-8') as file:
        file.writelines(unique_lines)

    # 如果你想要打印结果，可以取消下面的注释
    # for line in unique_lines:
    #     print(line, end='')  # end='' 防止print自动添加额外的换行符
del_chongfu()